package com.bawei.mall.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bawei.mall.product.domain.*;
import com.bawei.mall.product.mapper.*;
import com.bawei.mall.product.service.MallProductdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @Author：HaoYunde
 * @Package：com.bawei.mall.product.service.impl
 * @Project：mall_cloud
 * @name：MallProductdetailServiceImpl
 * @Date：2024/7/2 20:28
 */
@Service
public class MallProductdetailServiceImpl implements MallProductdetailService {
    @Autowired
    private MallProductdetailMapper productdetailMapper;
    @Autowired
    private MallProductBrandInfoMapper brandInfoMapper;
    @Autowired
    private MallProductInfoMapper productInfoMapper;
    @Autowired
    private MallProductSkuInfoMapper skuInfoMapper;
    @Autowired
    private MallProductAttributeValuelistMapper attributeValuelistMapper;
    @Autowired
    private MallProductAttributeMapper attributeMapper;

    @Override
    public MallProductDetail detail(Long productId) {
        // CompletableFuture 获取品牌信息
        CompletableFuture<MallProductBrandInfo> cfBrand = CompletableFuture.supplyAsync(() -> {
            MallProductInfo mallProductInfo = getProductInfo(productId);
            String brandId = mallProductInfo.getBrand();
            return brandInfoMapper.selectMallProductBrandInfoById(Long.valueOf(brandId));
        });

        // CompletableFuture 获取产品信息
        CompletableFuture<MallProductInfo> cfProductInfo = CompletableFuture.supplyAsync(() -> {
            return getProductInfo(productId);
        });

        // CompletableFuture 获取产品属性
        CompletableFuture<List<MallProductAttribute>> cfAttributes = cfProductInfo.thenApplyAsync(mallProductInfo -> {
            return attributeMapper.selectListByProductId(mallProductInfo.getRuleId());
        });

        // CompletableFuture 获取属性值列表
        CompletableFuture<List<MallProductAttributeValuelist>> cfAttributeValues = CompletableFuture.supplyAsync(() -> {
            return attributeValuelistMapper.selectListByProductId(productId);
        });

        // CompletableFuture 获取SKU信息
        CompletableFuture<List<MallProductSkuInfo>> cfSkus = CompletableFuture.supplyAsync(() -> {
            return skuInfoMapper.selectMallProductSkuInfoByProductId(productId);
        });

        // 结合所有的 CompletableFuture 并等待它们完成
        CompletableFuture.allOf(cfBrand, cfProductInfo, cfAttributes, cfAttributeValues, cfSkus).join();

        try {
            MallProductBrandInfo brandInfo = cfBrand.join();
            MallProductInfo productInfo = cfProductInfo.join();
            List<MallProductAttribute> attributes = cfAttributes.join();
            List<MallProductAttributeValuelist> attributeValues = cfAttributeValues.join();
            List<MallProductSkuInfo> skus = cfSkus.join();

            MallProductDetail mallProductDetails = new MallProductDetail();
            mallProductDetails.setBrand(brandInfo);
            mallProductDetails.setProduct(productInfo);
            mallProductDetails.setProductAttributeList(attributes);
            mallProductDetails.setProductAttributeValueList(attributeValues);
            // 处理sku，转格式
            for (MallProductSkuInfo mallProductSkuInfo : skus) {
                String sku = mallProductSkuInfo.getSku();
                String[] split = sku.split(",");
                List<Map> type=new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    Map map = new HashMap<>();
                    map.put("key",i+1);
                    map.put("value",split[i]);
                    type.add(map);
                }
                //[{\"key\":\"颜色\",\"value\":\"蓝色\"},{\"key\":\"尺寸\",\"value\":\"X\"}]转格式
                mallProductSkuInfo.setSku(JSON.toJSONString(type));
            }
            mallProductDetails.setSkuStockList(skus);

            return mallProductDetails;
        } catch (Exception e) {
            throw new RuntimeException("获取产品详情失败", e);
        }
    }


    private MallProductInfo getProductInfo(Long productId) {
        MallProductInfo mallProductInfo = productInfoMapper.selectMallProductInfoById(productId);
        if (mallProductInfo == null) {
            throw new RuntimeException("找不到ID为 " + productId + " 的产品");
        }
        return mallProductInfo;
    }


    @Override
    public MallProductBrandInfo detail2(Long id) {
        //获得商品信息
        MallProductInfo mallProductInfo = productInfoMapper.selectMallProductInfoById(id);
        //拿到品牌id
        String brand = mallProductInfo.getBrand();
        //拿到品牌信息
        MallProductBrandInfo mallProductBrandInfo = brandInfoMapper.listBybrandId(Integer.valueOf(brand));
        return mallProductBrandInfo;
    }

}
